package com.bw.shop_2302.update

import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.base.net.Cons
import com.bw.shop_2302.R
import com.bw.shop_2302.databinding.ActivityUpdateBinding

class UpdateActivity : BaseActivity<ActivityUpdateBinding,UpdateViewModel>() {
    override fun initView() {

        binding.tvCode.setOnClickListener {

            viewModel.sendcode(SPUtils.getInstance().getString(Cons.SP_PHONE))


        }

        binding.btUpdate.setOnClickListener {

            val toString = binding.etPassword.text.toString()

            var regex="^(?=.*\\d)(?=.*[a-zA-Z])[\\da-zA-Z]{6,8}\$".toRegex()
            if(!toString.matches(regex)){
                ToastUtils.showLong("密码格式错误")
                return@setOnClickListener
            }

            viewModel.update(SPUtils.getInstance().getString(Cons.SP_PHONE),binding.etCode.text.toString(),binding.etNewpassword.text.toString())



        }
    }

    override fun initData() {

        viewModel.sendsuccess.observe(this){

            if(it.code==200){
                binding.etCode.setText(it.data.content.substring(8,14))
            }

        }

        viewModel.updatesuccess.observe(this){
            if(it.code==200){
                ToastUtils.showLong(it.message)
                finish()
            }
            ToastUtils.showLong(it.message)
        }

    }

    override fun getviewmodel(): Class<UpdateViewModel> = UpdateViewModel::class.java

    override fun getLayoutID(): Int = R.layout.activity_update

}